package com.prs.crm.service.sms.impl;

import java.util.Collection;
import java.util.List;
import java.util.UUID;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.sms.SmsSendDao;
import com.prs.crm.domain.sms.SmsSend;
import com.prs.crm.exception.InValidException;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.sms.SmsSendService;

public class SmsSendServiceImpl extends BaseServiceImpl<SmsSend,SmsSendDao> implements SmsSendService{

	@Override
	public Collection<SmsSend> getSendingSms() {
		// TODO Auto-generated method stub
		return this.getDao().getSendingSms();
	}

	@Override
	public void submitSmsSend(String telephoneNo, String message) {
		// TODO Auto-generated method stub
		if(!ToolKits.isTelephoneNo(telephoneNo)) {
			throw new InValidException("手机号码不正确，请检查!");
		}
		
		SmsSend send = new SmsSend();
		send.setTelephoneNo(telephoneNo);
		send.setMessage(message);
		send.setMessageId(UUID.randomUUID().toString());
		send.setState(Constrains.SMS_SENDING);
		
		List<SmsSend> sends = ToolKits.pageSms(send);
		for(SmsSend smsSend : sends) {
			this.getDao().save(smsSend);
		}	
	}

	@Override
	public void submitSmsSend(String[] telephoneNos, String message) {
		// TODO Auto-generated method stub
		for(String phoneNo : telephoneNos) {
			if(!ToolKits.isTelephoneNo(phoneNo)) {
				throw new InValidException("手机号码不正确，请检查!");
			}
			
			SmsSend send = new SmsSend();
			send.setTelephoneNo(phoneNo);
			send.setMessage(message);
			send.setState(Constrains.SMS_SENDING);
			
			List<SmsSend> sends = ToolKits.pageSms(send);
			for(SmsSend smsSend : sends) {
				this.getDao().save(smsSend);
			}	
		}
	}
	
	

}
